package com.amoby.main.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.amoby.base.common.BaseAction;
import com.amoby.base.common.Constants;
import com.amoby.base.common.Pager;
import com.amoby.base.utils.DateUtil;
import com.amoby.base.utils.MD5Encrypt;
import com.amoby.main.entity.Business;
import com.amoby.main.entity.Channel;
import com.amoby.main.entity.SysUserRoleKey;
import com.amoby.main.entity.SysUsers;
import com.amoby.main.mapper.BusinessMapper;
import com.amoby.main.mapper.SysRolesMapper;
import com.amoby.main.mapper.SysUserRoleMapper;
import com.amoby.main.mapper.SysUsersMapper;
import com.amoby.main.service.BusinessService;

/**
 * ParamAction Desc：商户管理
 * @address http://www.yingmob.com/
 * @author zhaoyy
 * @date 2016年4月11日 上午10:44:37
 */
@Service
public class BusinessServiceImpl implements BusinessService {

	@Autowired
	private BusinessMapper businessMapper;
	@Autowired
	private SysUsersMapper sysUsersMapper;
	@Autowired
	private  SysRolesMapper roleDao;
	@Autowired
	private SysUserRoleMapper userRoleMapper;

	@Override
	public Pager query(Pager pager) {
		List<?> list = businessMapper.selectCountByPager(pager);
		pager.setResult(list);
		return pager;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public int add(Business business,Channel channel) {
		// 创建商家的时候同时创建商家登陆后台 的账号
		SysUsers user = new SysUsers();
		user.setUsername(business.getUsername());
		user.setNick(business.getFullName());
		user.setPassword(MD5Encrypt.MD5(Constants.DEFAULT_PASSWORD));
		user.setPhone(business.getPhone());
		user.setStatus(business.getStatus());
		user.setEmail(business.getEmail());
		user.setCreateDate(DateUtil.getCurDate());
		user.setUpdateDate(DateUtil.getCurDate());
		sysUsersMapper.saveUser(user);
		// 分配角色
		SysUserRoleKey userRole = new SysUserRoleKey();
		Integer roleId = roleDao.getRoleIdByRoleName(Constants.ROLE_NAME);
		userRole.setRoleId(roleId);
		userRole.setUserId(user.getUserId());
		userRoleMapper.insert(userRole);
		// 保存商家
		business.setPassword(MD5Encrypt.MD5(Constants.DEFAULT_PASSWORD));
		business.setCreateUser(BaseAction.getUser().getUserId());
		business.setCreateTime(new Date());
		business.setUserId(user.getUserId());
		business.setChannelId(channel.getId());  //渠道id
		business.setChannelName(channel.getChannelName()); //渠道名称
		business.setIsBatch(channel.getIsBatch());  //批量转账
		Integer result = businessMapper.insertSelective(business);
		return result;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public int del(Long id, Integer userId) {
		// 删除商家的时候 同步删除改商家的权限和登陆用户
		sysUsersMapper.deleteByPrimaryKey(userId);
		userRoleMapper.deleteByUserId(userId);
		return businessMapper.deleteUpdate(id);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public int upd(Business business) {
		SysUsers user = new SysUsers();
		user.setUserId(business.getUserId());
		user.setUsername(business.getUsername());
		user.setNick(business.getFullName());
		user.setPhone(business.getPhone());
		user.setStatus(business.getStatus());
		user.setEmail(business.getEmail());
		user.setUpdateDate(DateUtil.getCurDate());
		sysUsersMapper.updateByPrimaryKeySelective(user);
		business.setUpdateUser(BaseAction.getUser().getUserId());
		business.setUpdateTime(new Date());
		if (business.getShopSubclass() == null)
			business.setShopSubclass("");
		return businessMapper.updBusiness(business);
	}

	@Override
	public Business findById(Long id) {
		return businessMapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean check(String key, String val) {
		return businessMapper.checkField(key, val) > 0;
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public int reset_pwd(Long id, Integer userId) {
		// 重置商家密码的时候同时同步修改该商家登陆后台的密码 廖江红
		SysUsers user = new SysUsers();
		user.setUserId(userId);
		user.setPassword(MD5Encrypt.MD5(Constants.DEFAULT_PASSWORD));
		user.setUpdateDate(DateUtil.getCurDate());
		sysUsersMapper.updateByPrimaryKeySelective(user);
		// 重置商家密码
		Business business = new Business();
		business.setId(id);
		business.setPassword(MD5Encrypt.MD5(Constants.DEFAULT_PASSWORD));
		return businessMapper.updateByPrimaryKeySelective(business);
	}

	@Override
	public int checkUserNameById(String username, Long id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("username", username);
		map.put("id", id);
		return businessMapper.checkUserNameById(map);
	}
	
	@Override
	public int checkShopName(String shopName, Long id) {
	    Map<String, Object> map = new HashMap<String, Object>();
	    map.put("shopName", shopName);
	    map.put("id", id);
	    return businessMapper.checkShopName(map);
	}

	@Override
	public Pager queryBusinessByUserId(Pager pager) {
		List<?> list = businessMapper.queryBusinessByUserId(pager);
		pager.setResult(list);
		return pager;
	}

	// ==========================================支付宝商户开始================================================
	/**
	 * 支付宝商户列表分页
	 * @param pager
	 * @return
	 */
	@Override
	public Pager alipayQuery(Pager pager) {
		List<?> list = businessMapper.alipayQuery(pager);
		pager.setResult(list);
		return pager;
	}

	/**
	 * 更新商户二维码
	 */
	@Override
	public int updById(Map<String, Object> inputMap) {
		return businessMapper.updById(inputMap);
	}

	// ==========================================支付宝商户结束================================================

}
